<template>
  <div>
    <el-row type="flex" justify="center" align="middle" class="licc-home-shop-cart-top-row">
      <el-row type="flex" justify="center" align="middle"  style="width:1100px;">
        <el-col class="licc-goods-list-bread">
          我的购物车
        </el-col>
      </el-row>
    </el-row>

    <el-row type="flex" justify="center" align="middle" class="licc-home-shop-cart-top-row">
      <el-row type="flex" justify="center" align="middle"  style="width:1100px;">
        <el-table
          ref="multipleTable"
          :data="shopCarts"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            label="商品"
            width="300">
            <template slot-scope="scope">
              <div class="licc-goods-item">
                <div class="licc-p-img">
                  <router-link :to="{path:'/goodsInfo',query:{goodsId:scope.row.goodsId,catId:scope.row.catId}}" target="_blank">
                    <img width="80" :src="scope.row.url" title="图片解释">
                  </router-link>
                </div>
              </div>
              <div class="licc-item-msg">
                <div class="licc-p-name">
                  <router-link :to="{path:'/goodsInfo',query:{goodsId:scope.row.goodsId,catId:scope.row.catId}}" target="_blank">
                    {{scope.row.goodsName}}
                  </router-link>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="130"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="licc-props-txt">{{scope.row.productName}}</div>
            </template>
          </el-table-column>
          <el-table-column
            width="130">
            <template slot-scope="scope">
              <div class="licc-plus-switch">¥{{scope.row.shopPrice + scope.row.productPrice}}</div>
            </template>
          </el-table-column>
          <el-table-column
            width="160">
            <template slot-scope="scope">
              <div class="licc-quantity">
                <el-input-number :min="1" size="mini" v-model="scope.row.cartNum" @change="numberChange(scope.row)"></el-input-number>
                <div style="width:130px;color:#aaa;font-size:12px;text-align:center" v-show="(scope.row.cartNum>scope.row.productNumber)?false:true">有货</div>
                <div style="width:130px;color:#E2231A;font-size:12px;text-align:center" v-show="(scope.row.cartNum>scope.row.productNumber)?true:false">无货</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="120">
            <template slot-scope="scope">
              <div class="licc-plus-sum">¥{{(((scope.row.shopPrice + scope.row.productPrice)*100)*scope.row.cartNum)/100}}</div>
            </template>
          </el-table-column>
          <el-table-column
            width="205">
            <template slot-scope="scope">
              <div class="licc-plus-delete"><strong @click="shopCartDeleteOne(scope.$index, scope.row)">删除</strong></div>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-row>

    <el-row type="flex" justify="center" align="middle" class="licc-home-shop-cart-top-row">
      <el-row type="flex" justify="center" align="middle"  style="width:1100px;">
        <el-col class="licc-cart-bottom">
          <el-col class="licc-cart-operation">
            <strong @click="shopCartDeleteChoose">删除选中的商品</strong>
          </el-col>
          <el-col class="licc-cart-toolbar-right">
            <div class="btn-area">
              <!--<router-link :to="{path:'/addOrder',query:{shopCartIds:shopCartIds}}" class="licc-cart-submit-btn">去结算</router-link>-->
              <div @click=addOrderClick class="licc-cart-submit-btn" :class="{'licc-cart-submit-btn-add':addOrderDisable}">去结算</div>
            </div>
            <div class="price-sum">
              <span>总价：</span>
              <span class="licc-cart-price"><em>¥{{cartPrice.toFixed(2)}}</em></span>
            </div>
          </el-col>
        </el-col>
      </el-row>
    </el-row>
  </div>
</template>
<script>
import shopCartApi from '@/api/shopCart'
export default {
  name: 'page401',
  data() {
    return {
      /** 购物信息 **/
      shopCartIds: [],
      shopCarts: [],
      cartPrice: 0.00,
      multipleSelection: [],
      addOrderDisable: false,
    }
  },
  created(){
    this.initPage();
  },
  methods: {
    initPage(){
      this.shopCarts = [],
      shopCartApi.initShopCartList().then(res => {
        this.shopCarts = res.data;
      }).catch(() => {

      })
    },
    handleSelectionChange(vals) {
      /** 商品是否被勾选时的函数 **/
      this.multipleSelection = vals;
      let isClick = false;
      if (vals.length>0){
        vals.forEach((val, i) => {
          if (val.cartNum>val.productNumber) {
            isClick = true;
          }
        })
      }
      this.addOrderDisable = isClick?true:false;
      /** 商品选择变动 数量变动时总价及时更新工具类 **/
      this.changeAllPriceUtils();
    },
    numberChange(row){
      /** 购物车数量变更钩子 **/
      shopCartApi.updateShopCartInfoById({id: row.id, cartNum: row.cartNum}).then(res => {
        /** 由于是修改数量，这里不做提示 **/
      }).catch(() => {
        /** 由于是修改数量，这里不做提示 **/
      });
      /** 商品选择变动 数量变动时总价及时更新工具类 **/
      this.changeAllPriceUtils();
    },
    shopCartDeleteOne(index, row){
      /** 删除商品  **/
      this.$confirm('您将会从购物车删除该商品, 是否继续?', '删除商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        shopCartApi.deleteShopCart({id: row.id}).then(res => {
          this.$notify({title: '成功', message: '成功从购物车删除', type: 'success'});
          this.shopCarts.splice(index, 1);
        }).catch(() => {
          this.error({title: '失败', message: '删除失败，您可以稍后再试'});
        })
      }).catch(() => {
        this.$notify.info({title: '取消', message: '取消删除'});
      });
    },
    shopCartDeleteChoose(){
      /** 删除选中的商品  **/
      let infos = this.$refs.multipleTable.store.states.selection;
      let ids = [];
      infos.forEach((value, index) => {
        ids.push(value.id);
      });
      if (ids.length < 1){
        this.$notify({title: '提示', message: '请选择你要删除的商品', type: 'warning'});
        return false;
      }
      this.$confirm('您将会从购物车删除选中商品, 是否继续?', '删除商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        shopCartApi.deleteShopCartChoose({ids: ids}).then(res => {
          this.$notify({title: '成功', message: '成功删除商品', type: 'success'});
          this.initPage();
        }).catch(() => {
          this.error({title: '失败', message: '删除失败，您可以稍后再试'});
        })
      }).catch(() => {
        this.$notify.info({title: '取消', message: '取消删除'});
      });
    },
    addOrderClick(){
      if (this.addOrderDisable){
        this.$notify({title: '提示', message: '所选商品包含库存不足', type: 'warning'});
        return false;
      }
      if (this.shopCartIds.length === 0){
        /** 没有选择商品 **/
        this.$notify({title: '提示', message: '请选择商品', type: 'warning'});
      } else {
        this.$router.push({path:'/addOrder',query:{shopCartIds:this.shopCartIds}});
      }
    },
    changeAllPriceUtils(){
      /** 商品选择变动 数量变动时总价及时更新 **/
      let infos = this.$refs.multipleTable.store.states.selection;
      let allPirce = 0.00;
      this.shopCartIds = [];
      infos.forEach((value, index) => {
        allPirce = allPirce + ((value.shopPrice + value.productPrice)*value.cartNum);
        this.shopCartIds.push(value.id);
      });
      this.cartPrice = allPirce;
    }
  }
}
</script>
<style>
  .licc-home-shop-cart-top-row{
    width: 100%;
    margin-top: 20px;
  }

  .licc-goods-list-bread{
    width: 1100px;
    text-align: left;
    height: 50px;
    line-height: 50px;
    padding-left: 20px;
    background-color: #fff;;
  }
  /** 商品--开始 **/
  .licc-p-img {
    float: left;
    width: 80px;
    height: 80px;
    border: 1px solid #eee;
    margin-right: 10px;
    background: #fff;
    padding: 0;
    text-align: center;
    overflow: hidden;
  }
  .licc-p-img img{
    border: 0;
    margin: 0;
    padding: 0;
    vertical-align: middle;
  }
  .licc-item-msg{
    margin: 0;
    padding: 0;
    line-height: 20px;
  }
  .licc-p-name {
    margin: 0;
    padding: 0;
    height: 40px;
    line-height: 20px;
    overflow: hidden;
  }
  .licc-p-name a{
    color: #333;
  }

  .licc-props-txt{
    height: 80px;
    width: 130px;
    color: #333;
  }
  .licc-plus-switch{
    height: 80px;
    width: 110px;
    color: #333;
    text-align:right;
  }
  .licc-quantity{
    height: 80px;
    width: 140px;
  }
  .licc-plus-sum{
    height: 80px;
    width: 100px;
    color: #333;
    text-align:right;
  }
  .licc-plus-delete{
    height: 80px;
    width: 185px;
    color: #333;
    text-align:center;
  }
  .licc-plus-delete strong{
    cursor: pointer;
  }
  .licc-cart-operation strong:hover, .licc-p-name a:hover, .licc-plus-delete strong:hover{
    color: #E2231A;
  }
  /** 商品--结束 **/
  .licc-cart-bottom{
    width: 1100px;
    height: 50px;
    background-color: #fff;;
  }
  .licc-cart-operation{
    float: left;
    height: 50px;
    width: 300px;
    line-height: 50px;
  }
  .licc-cart-operation strong {
    float: left;
    font-weight:normal;
    margin-left: 10px;
    color: #666;
    font-size: 14px;
    cursor: pointer;
   }
  .licc-cart-toolbar-right{
    float: left;
    height: 50px;
    width: 800px;
    line-height: 50px;

  }
  .btn-area {
    float: right;
  }
  .licc-cart-submit-btn {
    display: block;
    position: relative;
    width: 94px;
    height: 50px;
    line-height: 50px;
    color: #fff;
    text-align: center;
    font-size: 18px;
    font-family: "Microsoft YaHei";
    background: #e54346;
    overflow: hidden;
    cursor: pointer;
  }
  .licc-cart-submit-btn-add{
    cursor: not-allowed;
  }
  .price-sum {
    float: right;
    height: 50px;
    line-height: 50px;
    margin: 0px 20px 0 10px;
    color: #666;
    width: auto;
    position: relative;
  }
  .licc-cart-price em {
    font-size: 16px;
    color: #E2231A;
    font-weight: 700;
  }
</style>
